// ==UserScript==
// @name         萌娘百科优化
// @namespace    https://gitcode.com/gagghik1
// @homepage     https://gitcode.com/gagghik1/greyz4k
// @version      1.8.0
// @description  对萌娘百科进行去广告处理 + 弹窗自动点击跳转
// @author       欧尼酱的可爱琳酱♡
// @match        *://*.moegirl.org.cn/*
// @icon         https://raw.gitcode.com/gagghik1/greyz4k/raw/main/moegirl/icon.png
// @grant        none
// @run-at       document-start
// @downloadURL  https://raw.gitcode.com/gagghik1/greyz4k/raw/main/moegirl/user.js
// @updateURL    https://raw.gitcode.com/gagghik1/greyz4k/raw/main/moegirl/meta.js
// ==/UserScript==

(() => {
    // ============================
    // 原有去广告逻辑
    // ============================

    const adSelectors = [
        'script[src*="google-analytics.com"]',
        'script[src*="googletagmanager.com"]',
        'script[src*="fundingchoicesmessages.google.com"]',
        '[class*="stevrhgmNo"]',
        '[class*="-WH4kRaZFv_"]',
        'a[href*="utm_source=adsmoe"]',
        '.n-message-container',
        '[class*="n-message-wrapper"]',
        '[class*="n-message--success-type"]',
        '[class*="tabler-icon-bell-plus"]',
        '[class*="订阅萌娘百科推送服务"]',
        '[class*="n-notification-wrapper"]',
        '[class*="n-notification--closable"]',
        '[class*="tabler-icon-bell-ringing"]',
        '[class*="有新的未读公告"]',
        '[class*="全站公告已移动到右上角的头像菜单中"]',
        '[class*="moe-card"][class*="X-"]',
        '#moe-global-background',
        '#moe-global-background-mask',
        '#moe-open-in-app',
        '[class*="n-button--primary-type"][class*="bottom-100px"]',
        '[href*="app.moegirl.org.cn"][href*="utm_source=moeskin_header"]',
        '[class*="n-marquee"]',
    ];

    const textChecks = [
        { selector: '[class*="n-marquee"]', text: '萌娘壁纸上新' },
    ];

    function checkElementText(el, text) {
        return el.textContent && el.textContent.includes(text);
    }

    function blockScripts() {
        const obs = new MutationObserver(mutations => {
            mutations.forEach(m => {
                m.addedNodes.forEach(node => {
                    if (node.tagName === 'SCRIPT') {
                        adSelectors.forEach(sel => {
                            if (node.matches(sel)) node.remove();
                        });
                    }
                });
            });
        });
        obs.observe(document, { childList: true, subtree: true });
    }

    function removeAds() {
        adSelectors.forEach(sel => {
            document.querySelectorAll(sel).forEach(el => el.remove());
        });

        textChecks.forEach(check => {
            document.querySelectorAll(check.selector).forEach(el => {
                if (checkElementText(el, check.text)) el.remove();
            });
        });
    }

    function addBlockingStyles() {
        const style = document.createElement('style');
        const cssSelectors = adSelectors.filter(
            sel => !textChecks.some(check => sel === check.selector)
        );

        style.textContent = `
            ${cssSelectors.join(', ')},
            [class*='ad-'], [id*='ad-'],
            [class*='gg-'], [id*='gg-'] {
                display: none !important;
                visibility: hidden !important;
                opacity: 0 !important;
                pointer-events: none !important;
                width: 0 !important;
                height: 0 !important;
                position: absolute !important;
                top: -9999px !important;
                left: -9999px !important;
            }
        `;
        document.head.appendChild(style);
    }

    function observeDOMChanges() {
        const start = () => {
            const obs = new MutationObserver(mutations => {
                if (mutations.some(m => m.addedNodes.length > 0)) removeAds();
            });
            obs.observe(document.body, { childList: true, subtree: true });
        };

        if (document.body) start();
        else document.addEventListener('DOMContentLoaded', start);
    }

    function init() {
        blockScripts();
        addBlockingStyles();
        removeAds();
        observeDOMChanges();

        window.addEventListener('load', removeAds);
        document.addEventListener('DOMContentLoaded', removeAds);
        window.addEventListener('scroll', removeAds);
        setInterval(removeAds, 1000);
    }

    init();

    // ============================
    // 新增：弹窗自动点击跳转模块
    // ============================

    // 融合逻辑：只要检测到广告弹窗按钮出现，立即触发点击
    const popupBtnSelectors = [
        '.FBSWGsM3f1_lxjtg3', // 弹窗“查看”按钮类名
        '.zeYQ0JE-Um_lxjtg3.FBSWGsM3f1_lxjtg3' // 可选备用
    ];

    const autoClickObserver = new MutationObserver(() => {
        for (const sel of popupBtnSelectors) {
            const btn = document.querySelector(sel);
            if (btn) {
                btn.click(); // 自动点击跳转
                return; // 点完就结束
            }
        }
    });

    autoClickObserver.observe(document.body, { childList: true, subtree: true });

})();
